package com.czn.minio_demo.controller;

import com.alibaba.fastjson.JSONObject;
import com.czn.minio_demo.config.MinioConfig;
import com.czn.minio_demo.entity.FileVo;
import com.czn.minio_demo.entity.Result;
import com.czn.minio_demo.utils.MinIoUtil;
import io.minio.messages.Bucket;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Base64Utils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/test")
@Api(tags = "测试controller")
public class testController {
    @Autowired
    MinioConfig minioConfig;

    /**
     * 压缩桶下的所有文件
     *
     * @param bucketName 桶名称
     * @param response
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "压缩桶下的所有文件", notes = "压缩桶下的所有文件")
    @ResponseBody
    @RequestMapping(value = "/getFileZip", method = RequestMethod.GET)
    public Object getFileZip(String bucketName, HttpServletResponse response) throws Exception {
        log.info("-------------------------------------> 压缩桶【{}】下的所有文件开始", bucketName);
        MinIoUtil.getFileZip(bucketName, response);
        log.info("-------------------------------------> 压缩桶【{}】下的所有文件结束。", bucketName);
        return "下载成功！";
    }

    /**
     * 获取文件列表
     *
     * @param pageNum  页码
     * @param pageSize 一页的数量
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "获取桶下的所有文件", notes = "获取桶下的所有文件")
    @ResponseBody
    @RequestMapping(value = "/fileList", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    public Object getFileList(String bucketName, Integer pageNum, Integer pageSize) throws Exception {
        log.info("-------------------------------------> 获取【{}】桶下的所有文件开始，分页参数pageNum：【{}】，pageSize：【{}】", bucketName, pageNum, pageSize);
        List<FileVo> fileList = MinIoUtil.getFileList(bucketName, pageNum, pageSize);
        log.info("-------------------------------------> 获取【{}】桶下的所有文件结束。");
        return fileList;
    }

    /**
     * 删除文件夹
     *
     * @param bucketName 桶名称
     * @return
     */
    @ApiOperation(value = "删除文件夹", notes = "删除文件夹")
    @RequestMapping(value = "deleteFolder", method = RequestMethod.GET)
    @ResponseBody
    public Object deleteFolder(String bucketName) {
        try {
            log.info("-------------------------------------> 删除文件夹开始，文件夹名称：【{}】", bucketName);
            MinIoUtil.deleteFolder(bucketName);
            log.info("-------------------------------------> 删除文件夹结束");
            return "删除文件夹成功！";
        } catch (Exception e) {
            log.error("-------------------------------------> 删除文件夹出错：", e.getMessage());
            return "删除文件夹失败：" + e.getMessage();
        }

    }

    /**
     * 获取所有桶
     *
     * @return
     */
    @ApiOperation(value = "获取所有桶", notes = "获取所有桶")
    @RequestMapping(value = "getAllBuckets", method = RequestMethod.GET)
    @ResponseBody
    public Object getAllBuckets() {
        log.info("-------------------------------------> 获取所有桶开始");
        List<Bucket> allBuckets = MinIoUtil.getAllBuckets();
        List<String> names = allBuckets.stream().map(a -> a.name()).collect(Collectors.toList());
        log.info("-------------------------------------> 所有桶：【{}】", names);
        log.info("-------------------------------------> 获取所有桶结束");
        return names;
    }

    /**
     * 上传文件到minio
     *
     * @param file 文件
     * @return
     */
    @ApiOperation(value = "上传文件到minio", notes = "上传文件到minio")
    @RequestMapping(value = "upload", method = RequestMethod.POST, headers = "content-type=multipart/form-data")
    @ResponseBody
    public Object upload(@ApiParam(value = "上传的文件", required = true) @RequestParam(value = "file", required = true) MultipartFile file) {
        log.info("-------------------------------------> 上传文件到minio开始");
        String httpUrl = MinIoUtil.upload(minioConfig.getBucketName(), file);
        log.info("-------------------------------------> 上传文件到minio结束");
        return httpUrl;
    }

    /**
     * 下载文件
     *
     * @param fileName 文件名
     * @param response
     * @throws IOException
     */
    @ApiOperation(value = "从minio下载文件", notes = "从minio下载文件")
    @GetMapping("downloadFile")
    public Object downloadFile(String fileName, HttpServletResponse response) {
        log.info("-------------------------------------> 从minio下载文件开始");
        try {
            MinIoUtil.download(minioConfig.getBucketName(), fileName, response);
            log.info("-------------------------------------> 从minio下载文件结束");
            return "下载成功！";
        } catch (Exception e) {
            log.error("-------------------------------------> 从minio下载文件：", e.getMessage());
            return "下载失败！";
        }
    }

    /**
     * 删除minio的文件
     *
     * @param fileName 文件名
     * @return
     */
    @ApiOperation(value = "删除minio的文件", notes = "删除minio的文件")
    @RequestMapping(value = "delete", method = RequestMethod.GET)
    @ResponseBody
    public Object delete(@ApiParam(value = "文件名称", required = true) @RequestParam(value = "fileName", required = true) String fileName) {
        log.info("-------------------------------------> 删除minio的文件开始");
        MinIoUtil.deleteFile(minioConfig.getBucketName(), fileName);
        log.info("-------------------------------------> 删除minio的文件结束");
        return "删除成功！";
    }

}